package com.shangyitong.yygh_hosp.service.impl.api;

import com.alibaba.fastjson.JSONObject;

import com.shangyitong.ResultCodeEnum;
import com.shangyitong.exception.HospitalException;
import com.shangyitong.helper.HttpRequestHelper;
import com.shangyitong.util.MapToJavaObjectWithClass;
import com.shangyitong.yygh.model.hosp.Department;
import com.shangyitong.yygh.model.hosp.HospitalSet;
import com.shangyitong.yygh_hosp.mapper.HospitalSetMapper;
import com.shangyitong.yygh_hosp.repository.DepartmentRepository;
import com.shangyitong.yygh_hosp.service.api.HospitalDepartmentService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.Date;
import java.util.Map;

@Service
@Slf4j
public class HospitalDepartmentServiceImpl implements HospitalDepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;
    @Autowired
    private HospitalSetMapper hospitalSetMapper;

    @Override
    public void saveDepartment(Map<String, String[]> parameterMap) {
        Department department = JSONObject.parseObject(MapToJavaObjectWithClass.mapToJavaObject(parameterMap), Department.class);
        String hoscode = department.getHoscode();
        if (StringUtils.isEmpty(hoscode)) {
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        String depcode = department.getDepcode();
        if (StringUtils.isEmpty(depcode)) {
            throw new HospitalException(ResultCodeEnum.DEPEND_IS_NULL);
        }
        HospitalSet hospitalSet = hospitalSetMapper.getByHoscode(hoscode);
        if (hospitalSet == null) {
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        Department hoscodeIsExist = departmentRepository.getByHoscodeAndDepcode(hoscode,
                depcode);
        if (hoscodeIsExist == null) {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            hoscodeIsExist.setUpdateTime(new Date());
            hoscodeIsExist.setIsDeleted(hospitalSet.getIsDeleted());
            departmentRepository.save(hoscodeIsExist);
        }
    }

    @Override
    public Page<Department> findDepartment(Map<String, String[]> parameterMap) {
        String hoscode = (String) MapToJavaObjectWithClass.switchObjectMap(parameterMap).get("hoscode");
        HospitalSet hospitalSet = hospitalSetMapper.getByHoscode(hoscode);
        if (hospitalSet == null) {
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        Integer pageNums = Integer.parseInt((String) MapToJavaObjectWithClass.switchObjectMap(parameterMap).get("page"));
        Integer limit = Integer.parseInt((String) MapToJavaObjectWithClass.switchObjectMap(parameterMap).get("limit"));
        Pageable pageable = PageRequest.of(pageNums - 1, limit);
        Page<Department> departmentPage = departmentRepository.findByHoscode(hoscode, pageable);
        return departmentPage;
    }

    @Override
    public void removeDepartment(Map<String, String[]> parameterMap) {
        Department department = JSONObject.parseObject(MapToJavaObjectWithClass.mapToJavaObject(parameterMap), Department.class);
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        if (StringUtils.isEmpty(hoscode)||StringUtils.isEmpty(depcode)){
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        Department departmentIsExist = departmentRepository.getByHoscodeAndDepcode(hoscode, depcode);
        if (departmentIsExist != null) {
            departmentRepository.deleteById(departmentIsExist.getId());
        } else {
            throw new HospitalException(ResultCodeEnum.DEPARTMENT_NOT_EXIST);
        }
    }
}
